const {chromium} = require('playwright');
const Segment = require('segment');

class AppBootHook {
  constructor(app) {
    this.app = app;
  }

  async configWillLoad() {
    if (!this.app.config.mini_assets) {
      this.app.config.coreMiddleware.unshift('less');
    }
  }

  async willReady() {
    this.app.logger.warn('app 准备启动...');

    let browser;

    const setup = async () => {
      browser = await chromium.launch({
        args: ['--disable-gpu', '--disable-dev-shm-usage', '--no-sandbox', '--disable-setuid-sandbox', '--disable-infobars', '--window-position=0,0', '--ignore-certificate-errors', '--ignore-certificate-errors-spki-list', `--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36`],
        headless: true,
        ignoreHTTPSErrors: true,
      });
      const context = await browser.newContext();
      browser.on('disconnected', setup);
      this.app.pptBrowser = context;
      this.app.logger.warn("初始化playwright chromium成功......");
    };

    await setup();

    const segment = new Segment();
    segment.useDefault();
    this.app.segment = segment;
    this.app.logger.warn("初始化segment成功......");
  }


  async serverDidReady() {
    if (!this.app.config.mini_assets) {
      // this.app.mongoose.set('debug', true);
    }
  }
}

module.exports = AppBootHook;
